package cn.edu.nuc.carsystem.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.nuc.carsystem.dto.PageInfo;
import cn.edu.nuc.carsystem.model.City;
import cn.edu.nuc.carsystem.model.Conrtuny;
import cn.edu.nuc.carsystem.service.interfaces.CityService;
/**
 * 城市controller
 * @author 杨晓庆
 *
 */
@RequestMapping("/car/city")
@Controller
public class CityController {
   @Autowired
   private CityService cityService;
   /**
    * 添加城市
    * @param city
    * @return
    */
   @RequestMapping(value="/insert",method=RequestMethod.POST)
	public String insert(City city){
    Integer count=cityService.insert(city);
    if(count>0){
	   return "redirect:/car/city/findAll";
    }
    else
       return "/car/city/addCity";
   }
   /**
    * 跳转添加城市页面
    * @param city
    * @return
    */
   @RequestMapping(value="/addCity",method=RequestMethod.GET)
	public String addCity(City city){
   
       return "car/addcity";
   }
   /**
    * 分页查询所有城市
    * @param current
    * @return
    */
   @RequestMapping(value="/findAll",method=RequestMethod.GET)
  	public ModelAndView findAll(@RequestParam(name="current",defaultValue="1") Integer current){
      ModelAndView model=new ModelAndView("car/citylist");
      PageInfo pi=cityService.findAll(current);
      model.addObject("pi",pi);
      System.out.println("citylist"+pi);
  	  return model;
  	}
   /*
    * 查询所有城市
    */
   @RequestMapping(value="/select",method=RequestMethod.POST)
   @ResponseBody
  	public List<City> select(){
	   System.out.println("citylist");
      List<City> list=cityService.select();

      System.out.println("citylist"+list);
  	  return list;
  	}


}
